package com.thraex.lucene.chapter1;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

/**
 * @Author 鬼王
 * @Date 2019/05/06 10:37
 */
public class Indexer {

    public static void main(String[] args) throws Exception {
        int len = 2;
        if (args.length != len) {
            throw new IllegalArgumentException("Usage: java " +
                Indexer.class.getName() + " <index dir> <data dir>");
        }

        //Scanner sc = new Scanner(System.in);

        // Note 1: Create index in this directory
        String indexDir = args[0];
        //System.out.println("Please input index directory");
        //String indexDir = sc.nextLine();

        // Note 2: Index *.txt files from this directory
        String dataDir = args[1];
        //System.out.println("Please input data directory");
        //String dataDir = sc.nextLine();

        long start = System.currentTimeMillis();
        Indexer indexer = new Indexer(indexDir);
        int numIndexed;
        try {
            numIndexed = indexer.index(dataDir, new TextFilesFilter());
        } finally {
            indexer.close();
        }
        long end = System.currentTimeMillis();

        System.out.println("Indexing " + numIndexed + " files took "
            + (end - start) + " milliseconds");
    }

    private IndexWriter writer;

    public Indexer(String indexDir) throws IOException {
        // Note 3: Create Lucene IndexWriter
        Directory dir = FSDirectory.open(new File(indexDir));
        writer = new IndexWriter(dir, new StandardAnalyzer(Version.LUCENE_30),
                true, IndexWriter.MaxFieldLength.UNLIMITED);
        writer.setInfoStream(System.out);
    }

    public void close() throws IOException {
        // Note 4: Close IndexWriter
        writer.close();
    }

    public int index(String dataDir, FileFilter filter) throws Exception {
        File[] files = new File(dataDir).listFiles();

        for (File f:files) {
            boolean is = !f.isDirectory() && !f.isHidden() && f.exists() && f.canRead() &&
                    (filter == null || filter.accept(f));
            if (is) { indexFile(f); }
        }

        // Note 5: Return number of documents indexed
        return writer.numDocs();
    }

    private static class TextFilesFilter implements FileFilter {

        @Override
        public boolean accept(File pathname) {
            // Note 6: Index .txt files only, using FileFilter
            return pathname.getName().toLowerCase().endsWith(".txt");
        }
    }

    protected Document getDocument(File f) throws Exception {
        Document doc = new Document();

        // Note 7: Index file content
        doc.add(new Field("contents", new FileReader(f)));

        // Note 8: Index filename
        doc.add(new Field("filename", f.getName(),
                Field.Store.YES, Field.Index.NOT_ANALYZED));

        // Note 9: Index file full path
        doc.add(new Field("fullpath", f.getCanonicalPath(),
                Field.Store.YES, Field.Index.NOT_ANALYZED));

        return doc;
    }

    private void indexFile(File f) throws Exception {
        System.out.println("Indexing " + f.getCanonicalPath());
        Document doc = getDocument(f);

        // Note 10: Add document to Lucene index
        writer.addDocument(doc);
    }

}
